<?php
namespace app\web\controller\admin;
use app\admin\library\AdminController;

class Page extends AdminController{
    public function onIndex(){
        $this->pageTitle = '页面管理';
        $this->pageIcon = 'fa-file-o';
        return $this->display();
    }

    public function onList(){
        $page = $this->model('Page');
        $pageCurrent = $this->post('page',$this->check('当前页')->default(1)->type('int'));
        $keyword =  $this->post('keyword',$this->check()->type('string'));
        
        $model = $this->model('Model');

        $page->field();
        $page->field($model->getField(['title'=>'model_title']));
        $page->join($model,$page->getField('model_id') .' = '.$model->getKey());

        if ($keyword){
            $page->where('title','like','%'.$keyword.'%');
        }

        $re['list'] = $page->page($pageCurrent,20)->order('page_id','DESC')->getData();
        $re['count'] = $page->pageCount;
        $re['total'] = $page->total;
        return $re;
    }


    public function onEdit(){
        
        $id = $this->get('id',$this->check('ID')->type('int'));


        if ($id){
            $this->pageTitle = '编辑页面';

            $page = $this->model('Page');


            if ($pageData = $page->getId($id)){

                try{
                    $param = json_decode($pageData['param'],true);
                }catch(\Exception $e){
                    $param = [];
                }


                try{
                    $pageData['request'] = json_decode($pageData['request'],true);
                }catch(\Exception $e){
                    $pageData['request'] = [];
                }

                $this->set('page',$pageData);
                $this->set('param',$param);
            }else{
                throw new \Exception('页面不存在!');
            }

        }else{
            $this->pageTitle = '添加页面';
        }

        return $this->display();

    }


    public function onSave(){
        $this->power('web.page');
        $id = $this->post('page_id',$this->check('ID')->type('int'));

        $data['title'] = $this->post('title',$this->check('标题')->type('string')->required());
        $data['route'] = $this->post('route',$this->check('路由')->type('string'));
        $data['route_type'] = $this->post('route_type',$this->check()->type('string'));
        $data['model_id'] = $this->post('model_id',$this->check()->type('int'));
        $data['template'] = $this->post('template',$this->check()->type('string'));

        if ($data['route_type']!='default'){
            if ($data['route']==''){
                throw new \Exception('路由不能为空');
            }
        }

        $data['param'] = json_encode($this->post('param',[]));
        $data['request'] = json_encode($this->post('request',[]));

        $part = $this->model('Page');

        if ($id){
            if ($part->updateId($id,$data)){
                $this->log('编辑页面', ['page_id'=>$id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('编辑页面失败');
            }
        }else{
           if ($page_id = $part->insert($data)){
                $this->log('添加页面', ['page_id'=>$page_id,'title'=>$data['title']]);
                return true;
           }else{
               throw new \Exception('添加页面失败');
           }
        }
    }

    public function onDel(){
        $this->power('web.page');
        
        $id = $this->post('page_id',$this->check('ID')->type('int')->required());
        
        $page = $this->model('Page');

        $pageData = $page->getId($id);
        if (!$pageData){
            throw new \Exception("页面不存在");
        }
        
        if ($page->deleteId($id)){
            $this->log('删除页面', ['page_id'=>$id,'title'=>$pageData['title']]);
            return true;
        }else{
            throw new \Exception('删除页面失败');
        }
    }
}